<?php
namespace app\hotel\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\hotel\model\Hotel;
class Index extends Common
{
    public function index()
    {
        $admin_id   = session('h_admin_id');
        $admin_pid   = session('h_admin_pid');
        if ($admin_pid == '0'){
            $nav = Db::name('hotel_auth_rule')->field('id,pid,title,icon,name')->where("ismenu = 1")->order("id asc")->select();
        }else{
            //获取所有权限的菜单栏
            $Auth= new \AuthHotel();
            $admin_rules = $Auth->getGroups($admin_id);
            $rules = explode(',',$admin_rules[0]['rules']);
            //获取导航栏列表
            $where['id'] = array('in',$rules);
            $where['ismenu'] = 1;
            $nav = Db::name('hotel_auth_rule')->field('id,pid,title,icon,name')->where($where)->order("id asc")->select();
        }

        foreach($nav as $key => $value){
            if ($value['pid'] == 0){
                $data[$key]['title'] = $value;
                foreach($nav as $key2 => $value2){
                    if ($value2['pid'] == $value['id']){
                        $data[$key]['nav'][$key2] =$value2;
                        $data[$key]['nav'][$key2]['name'] = url($value2['name']);
                    }
                }
            }
        }

        $this->assign('nav',$data);
        //获取数据
        $img = Db::name('hotel_img')->where('hotel_id',Session::get('hotel_id'))->count();
        $room = Db::name('hotel_room')->where('hotel_id',Session::get('hotel_id'))->select();

        if($img<3 || empty($room)){
            Db::name('hotel')->where('hotel_id',Session::get('hotel_id'))->setField('is_display',1);
            $this->assign('type',1);
        }else{
            Db::name('hotel')->where('hotel_id',Session::get('hotel_id'))->setField('is_display',0);
            $this->assign('type',2);
        }
        $res = Db::name('hotel')->where('hotel_id',Session::get('hotel_id'))->find();
        $this->assign('res',$res);
        return view();

    }

    public function logout(){
        Session::clear();
        if (!Session::has('t_admin_id')){
            return ['code'=>0];
        }else{
            return ['code'=>1];
        }
    }

    public function editPassword(){
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $saveData['password'] = $hasher->HashPassword($post['data']['admin_password']);
            $saveData['h_admin_id'] = Session::get('h_admin_id');
            $res = Db::name('hotel_admin')->update($saveData);
            if ($res){
                Session::clear();
                return ['code'=>0];
            }else{
                return ['code'=>1];
            }
        }
    }
    //是否上架
    public function up()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            //查询是否有商品 图片的数量
            $img = Db::name('hotel_img')->where('hotel_id',Session::get('hotel_id'))->count();
            $room = Db::name('hotel_room')->where('hotel_id',Session::get('hotel_id'))->select();
            if($img<3 || empty($room)){
                return ['code'=>2,'msg'=>'商品或酒店相册没有完善'];
            }else{
                Db::name('hotel')->where('hotel_id',Session::get('hotel_id'))->setField('is_display',0);
                return ['code'=>0];
            }
        }
    }
}
